package com.heima.article.feign;

import com.heima.apis.article.IArticleClient;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.service.ApArticleConfigService;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.comment.dtos.OpenOrCloseCommentDTO;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.StatisticsDto;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author itlxc
 * @since 2024-07-27
 */
@RestController
@Slf4j
public class ArticleClient implements IArticleClient {

    @Autowired
    private ApArticleService apArticleService;

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    /**
     * 保存app端相关文章
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/api/v1/article/save")
    public ResponseResult saveArticle(@RequestBody ArticleDto dto) {
        log.info("保存app端相关文章:{}", dto);
        return apArticleService.saveArticle(dto);
    }


    @Override
    @GetMapping("/api/v1/article/{articleId}")
    public ResponseResult findArticleConfigByArticleId(Long articleId) {
        ApArticleConfig apArticleConfig = apArticleConfigService.lambdaQuery()
                .select(ApArticleConfig::getIsComment)
                .eq(ApArticleConfig::getArticleId, articleId)
                .one();
        return ResponseResult.okResult(apArticleConfig);
    }

    @Override
    @PostMapping("/api/v1/article/update/comment/status")
    public ResponseResult updateCommentStatus(OpenOrCloseCommentDTO dto) {
        boolean flag = true;
        if (dto.getOperation() == 0) {
            flag = false;
        }
        apArticleConfigService.lambdaUpdate()
                .set(ApArticleConfig::getIsComment, flag)
                .eq(ApArticleConfig::getArticleId, dto.getArticleId())
                .update();
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @GetMapping("/api/v1/article/queryLikesAndConllections")
    @Override
    public ResponseResult queryLikesAndConllections(@RequestParam("wmUserId") Integer wmUserId, @RequestParam("beginDate") Date beginDate, @RequestParam("endDate") Date endDate) {
        return apArticleService.queryLikesAndConllections(wmUserId,beginDate,endDate);
    }


    @PostMapping("/api/v1/article/newPage")
    @Override
    public PageResponseResult newPage(@RequestBody StatisticsDto dto) {
        return apArticleService.newPage(dto);
    }
}
